<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "tbl_jobs".
 *
 * @property int $id
 * @property string $name 名称
 * @property int $cate 企业类型：1工厂，2公司
 * @property int $pid 上级id，可分为2级：部门、岗位
 * @property int $del 删除：0未删除，1删除
 * @property int $created_at
 * @property int $updated_at
 */
class Jobs extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'tbl_jobs';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['pid', 'time', 'updated_at'], 'integer'],
            [['time', 'updated_at'], 'required'],
            [['name'], 'string', 'max' => 50],
            [['cate', 'del'], 'string', 'max' => 1],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'cate' => 'Cate',
            'pid' => 'Pid',
            'del' => 'Del',
            'time' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }


    /***
     * 返回职位json数据
    */
    public static function Job_list(){
        $positionList=self::find()->where([
            'cate'=>2,
            'pid'=>0,
            'del'=>0
        ])->asArray()->all();
        $data = [];
            foreach ($positionList as $k => $v) {
                $jobs = self::find()->where([
                    'cate'=>2,
                    'pid'=>$v['id']
                ])->select(['id', 'name'])->asArray()->all();
                $data[$k]['position'] = [];
                $data[$k]['position']['name'] = $v['name'];
                $data[$k]['position']['job'] = $jobs;
            }
        return $data;
       /// print_r($data);exit;
    }




}
